#include <sys_config.h>

#ifdef HILINK_SUPPORT
#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libpub/lib_frontend.h>

#include "osdobjs_def.h"
#include "osd_config.h"
#include "string.id"
#include "images.id"
#include "menus_root.h"
#include "win_com.h"

#include <hld/ali_3g_mdm/ali_3g_mdm_api.h>
#include <api/libhilink/lib_hilink.h>
#include "copper_common/com_api.h"

#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#ifndef SD_UI
#ifdef	_BUILD_OTA_E_
#define	W_W         692             //493
#define	W_H         488             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         98              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 48)
#else

#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#endif

//#define CON_L		W_L


#define CON_H		40
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		260
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2
#define LIST_ITEMCNT 7
#else
#ifdef	_BUILD_OTA_E_
#define	W_W         482             //493
#define	W_H         360             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         60              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 40)
#else
#define	W_L     	105//384
#define	W_T     	57//138
#define	W_W     	482
#define	W_H     	370
#define CON_L		(W_L+20)
#define CON_T		(W_T + 8)
#define CON_W		(W_W - 40)
#endif

#define CON_H		30
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		180
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2
#define LIST_ITEMCNT 7

#endif
extern CONTAINER g_win_hilink_automode;

extern CONTAINER g_win_hilink_auto_connnect_con;

extern TEXT_FIELD hilink_auto_connect_text;

extern MULTISEL hilink_auto_connect_mul;

extern TEXT_FIELD hilink_auto_connect_line;

static VACTION win_hilink_automode_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_hilink_automode_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION win_hilink_automode_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_hilink_automode_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION win_hilink_automode_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_hilink_automode_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    win_hilink_automode_item_con_keymap,win_hilink_automode_item_con_callback,  \
    conobj, ID,1)
    
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl)	\
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    win_hilink_automode_item_sel_keymap,win_hilink_automode_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,ptabl,cur,cnt)
    

#define LDEF_MENU_ITEM(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,l,t,w,h,\
						resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL/*&varLine*/   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    win_hilink_automode_keymap,win_hilink_automode_callback,  \
    nxtObj, focusID,0)

static UINT16 hilink_auto_connect_ids[] = 
{
	RS_COMMON_ON,
	RS_COMMON_OFF
};

HILINK_AUTOMODE_INFO_STRU hilink_automode_info = {0};

typedef enum
{
	HILINK_AUTO_CONNECT_ID = 1,
}HILINK_AUTOMODE_ID;

LDEF_MENU_ITEM(g_win_hilink_automode,g_win_hilink_auto_connnect_con,NULL,hilink_auto_connect_text,hilink_auto_connect_mul,hilink_auto_connect_line, 1, 1, 1, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_HILINK_AUTO_CONNECT, \
		STRING_ID, 0, sizeof(hilink_auto_connect_ids)/sizeof(UINT16), hilink_auto_connect_ids)

LDEF_WIN(g_win_hilink_automode,&g_win_hilink_auto_connnect_con,W_L, W_T, W_W, W_H, 1)

static VACTION win_hilink_automode_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
    
	switch(key)
	{
	  	case V_KEY_UP:
	        act = VACT_CURSOR_UP;
	        break;
	    case V_KEY_DOWN:
	        act = VACT_CURSOR_DOWN;
			break;
		case V_KEY_EXIT:
		case V_KEY_MENU: 
			act = VACT_CLOSE;
			break;
		default:
			act = VACT_PASS;
	}

	return act;	
}

static void win_hilink_automode_load_setting(void)
{
	MULTISEL *msel = NULL;

    hilink_getautomodeinfo_fromserver(&hilink_automode_info);

	msel = &hilink_auto_connect_mul; 
	OSD_SetMultiselSel(msel, hilink_automode_info.ulautomode);
}

static PRESULT win_hilink_automode_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	CONTAINER* root = (CONTAINER*) pObj;
	
	switch(event)
	{
		case EVN_PRE_OPEN:
			win_hilink_automode_load_setting();
			wincom_open_title(pObj, RS_HILINK_AUTOMODE, 0);
			break;
		case EVN_POST_OPEN:
			break;
		case EVN_PRE_CLOSE:
			sys_data_save(1);
			*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG; /* Make OSD not flickering */
			break;
		case EVN_POST_CLOSE:
			break;
        case EVN_MSG_GOT:
            if ((CTRL_MSG_SUBTYPE_CMD_EXIT == param1) || (CTRL_MSG_SUBTYPE_CMD_EXIT_ALL == param1))
            {
                ret = PROC_LEAVE;
            }    
            if ((CTRL_MSG_SUBTYPE_CMD_HILINK == param1) && (HILINK_STATUS_PLUGOUT == param2))
            {
                ret = PROC_LEAVE;
                ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, HILINK_STATUS_PLUGOUT, FALSE);
            }
            break;
		default:
			break;
	}

	return ret;
}

static VACTION win_hilink_automode_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	return act;	
}

static PRESULT win_hilink_automode_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	return ret;
}

static VACTION win_hilink_automode_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
	switch(key)
	{
	  	case V_KEY_LEFT:
	        act = VACT_DECREASE;
	        break;
	    case V_KEY_RIGHT:
	        act = VACT_INCREASE;
			break; 
		default:
			act = VACT_PASS;
			break;
	}

	return act;	
}

static PRESULT win_hilink_automode_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 sel = 0;
	UINT8 bID = 0;
	UINT32 drawCmd = C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL;

	bID = OSD_GetObjID(pObj);

	switch(event)
	{
		case EVN_PRE_CHANGE:
			sel = *((UINT32*)param1);
			break;
		case EVN_POST_CHANGE:
			sel = param1;
			if (HILINK_AUTO_CONNECT_ID == bID)
			{
                hilink_automode_info.ulautomode = sel;
                hilink_setautomodeinfo_toserver(hilink_automode_info);
			}
	        break;
		default:
			break;
	}
 	return ret;
}

#endif

